<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- http://api.xiaohigh.com -->
    <button>王者荣耀</button>
    <button>英雄联盟</button>
    <hr>
    <!-- http://127.0.0.1:3000 -->
    <button>电影</button>
    <button>图书</button>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <script>
        // axios.defaults.baseURL = 'http://api.xiaohigh.com'

        // 当一个前端项目需要请求多个后台服务的接口时, 必须使用create()
        // 实际项目: 都会用create
        // create方法返回是一个函数

        const newAxios1 = axios.create({
            baseURL: 'http://api.xiaohigh.com',
            timeout: 30000
        })

        const newAxios2 = axios.create({
            baseURL: 'http://127.0.0.1:3000',
            timeout: 40000
        })

        const btns = document.querySelectorAll('button');
        btns[0].onclick = async () => {
            newAxios1.get('/heros')
        }
        btns[1].onclick = async () => {
            newAxios1.get('/lol')
        }
        btns[2].onclick = async () => {
            newAxios2('/movie')
        }
        btns[3].onclick = async () => {
            newAxios2({
                method:'POST',
                url:'/book',
                data:{
                    title:'骆驼祥子',
                    author:'老舍'
                }
            })
            // const response = newAxios2({
            //     method: 'put',
            //     url: '/book/3',
            //     data: {

            //         title: '平凡的世界',
            //         author: '路遥'
            //     }
            // })
            // console.log(response.data)
        }
    </script>
</body>

</html>